//
//  HSMainAskVM.m
//  HolyShit
//
//  Created by Robin on 2017/3/29.
//  Copyright © 2017年 HolyShit. All rights reserved.
//

#import "HSMainAskVM.h"

@interface HSMainAskVM ()

@property (nonatomic, copy) NSString *type;
@property (nonatomic, assign) NSUInteger pos;

@end

@implementation HSMainAskVM

- (instancetype)init
{
    if (self = [super init]) {
        switch (appType) {
            case HSAppTypeCaiPiao:
                _type = @"finance";
                break;
            case HSAppTypeYuLe:
                _type = @"entertainment";
                break;
            case HSAppTypeTiYu:
                _type = @"sport";
                break;
            default:
                break;
        }
    }
    return self;
}

- (RACCommand *)refreshCmd{
    if(!_refreshCmd){
        @weakify(self);
        _refreshCmd = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            NSString *url = [NSString stringWithFormat:@"c.m.163.com/newstopic/list/expert/classification/%@/%d-10.html",self.type,self.pos];
            return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                if ([dict containsObjectForKey:@"data"]) {
                    NSDictionary *data = dict[@"data"];
                    @strongify(self);
                    self.pos = 0;
                    self.datas = [NSArray modelArrayWithClass:[HSMainAskModel class] json:data[@"expertList"]].mutableCopy;
                }
            } failed:^(NSError *error) {
                
            }].racSignal;
        }];
    }
    return _refreshCmd;
}

- (RACCommand *)loadmoreCmd{
    if(!_loadmoreCmd){
        @weakify(self);
        _loadmoreCmd = [RACCommand commandWithSignalBlock:^RACSignal *(id input) {
            @strongify(self);
            NSString *url = [NSString stringWithFormat:@"c.m.163.com/newstopic/list/expert/classification/%@/%d-10.html",self.type,self.pos + 10];
            return [[MYHttpHelper shareHelper] rac_getWithUrl:url params:nil success:^(NSDictionary *dict) {
                if ([dict containsObjectForKey:@"data"]) {
                    NSDictionary *data = dict[@"data"];
                    @strongify(self);
                    NSArray *array = [NSArray modelArrayWithClass:[HSMainAskModel class] json:data[@"expertList"]];
                    if (array.count > 0) {
                        self.pos += 10;
                        [self.datas addObjectsFromArray:array];
                    }
                }
            } failed:^(NSError *error) {
                
            }].racSignal;
        }];
    }
    return _loadmoreCmd;
}

@end
